import os
import csv
import time
import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import (
    NoSuchElementException,
    TimeoutException,
    WebDriverException,
)

def setup_logging(page_number, log_folder):
    """
    设置日志记录器，为每一页创建单独的日志文件。
    """
    logger = logging.getLogger(f"scraper_page_{page_number}")
    logger.setLevel(logging.INFO)
    
    # 避免重复添加处理器
    if not logger.handlers:
        log_file = os.path.join(log_folder, f"scraper{page_number}.log")
        handler = logging.FileHandler(log_file, encoding='utf-8')
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
    
    return logger

def initialize_driver():
    """
    初始化Selenium WebDriver。
    """
    try:
        chrome_driver_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
        options = webdriver.ChromeOptions()
        options.add_argument("--start-maximized")
        service = Service(chrome_driver_path)
        # 可选：无头模式
        # options.add_argument("--headless")
        driver = webdriver.Chrome(service=service,options=options)
        return driver
    except WebDriverException as e:
        print(f"初始化WebDriver时出错: {e}")
        exit(1)

def get_usernames(driver, logger):
    """
    从 https://coomer.su/artists/updated 页面提取用户名列表。
    """
    usernames = []
    try:
        # 等待用户卡片加载完成
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, "card-list.card-list--phone"))
        )
        card_list = driver.find_element(By.CLASS_NAME, "card-list.card-list--phone")
        a_tags = card_list.find_elements(By.TAG_NAME, "a")
        logger.info(f"当前页面找到 {len(a_tags)} 个用户卡片。")

        for a_tag in a_tags:
            try:
                user_card_name = a_tag.find_element(By.CLASS_NAME, "user-card__name")
                username = user_card_name.text.strip()
                if username:
                    usernames.append(username)
                    logger.info(f"找到用户名: {username}")
            except NoSuchElementException:
                logger.warning("找到一个没有名称的用户卡片，已跳过。")
    except TimeoutException:
        logger.error("等待用户卡片加载超时。")
    except Exception as e:
        logger.error(f"提取用户名时发生意外错误: {e}")

    return usernames

def get_user_contact(driver, username, logger):
    """
    访问 https://onlyfans.com/{username} 并提取联系信息。
    """
    contact_info = []
    url = f"https://onlyfans.com/{username}"
    logger.info(f"正在处理用户: {username}，访问链接: {url}")
    try:
        driver.get(url)
        time.sleep(2)  # 保持原有等待时间

        # 等待目标元素加载完成
        parent_element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, '/html/body/div/div[2]/main/div[1]/div[1]/div/div[2]/div/div[2]/div[2]/div[2]'))
        )

        # 找到目标路径下的所有 <a> 标签
        a_tags = parent_element.find_elements(By.TAG_NAME, "a")

        # 遍历每个 <a> 标签
        for index, a_tag in enumerate(a_tags, start=1):
            href = a_tag.get_attribute("href")
            span_elements = a_tag.find_elements(By.TAG_NAME, "span")
            span_texts = [span.text for span in span_elements]

            contact_str = "{}:{}".format(span_texts, href)
            contact_info.append(contact_str)
            logger.info(f"联系信息 {index}: {contact_str}")
    except TimeoutException:
        logger.error(f"在 {url} 等待联系信息时超时。")
        contact_info.append("超时未找到联系信息")
    except NoSuchElementException:
        logger.warning(f"在 {url} 找不到联系信息。")
        contact_info.append("无联系信息")
    except Exception as e:
        logger.error(f"在提取 {url} 的联系信息时发生错误: {e}")
        contact_info.append(f"错误: {e}")
    finally:
        # 返回主页面
        driver.back()
        time.sleep(2)  # 保持原有等待时间

    return contact_info

def write_to_csv(data, filename, folder):
    """
    将收集的数据写入CSV文件。
    """
    try:
        file_path = os.path.join(folder, filename)
        with open(file_path, mode="w", newline="", encoding="utf-8") as file:
            writer = csv.DictWriter(file, fieldnames=["username", "link", "contact"])
            writer.writeheader()
            writer.writerows(data)
        print(f"数据已写入 {file_path}")
    except Exception as e:
        print(f"写入CSV时出错: {e}")

def main():
    # 创建 usedata 和 log 文件夹
    usedata_folder = "usedata"
    log_folder = "log"
    os.makedirs(usedata_folder, exist_ok=True)
    os.makedirs(log_folder, exist_ok=True)

    # 初始化 WebDriver
    driver = initialize_driver()
    driver.get("https://coomer.su/artists/updated")  # 替换为目标网址
    driver.maximize_window()
    time.sleep(5)  # 保持原有等待时间

    try:
        for page_number in range(1, 1001):  # 处理1000页
            logger = setup_logging(page_number, log_folder)
            logger.info(f"开始处理第 {page_number} 页。")

            # 提取用户名
            usernames = get_usernames(driver, logger)
            if not usernames:
                logger.warning("未找到用户名，跳过此页。")
                break

            data = []
            for username in usernames:
                contact = get_user_contact(driver, username, logger)
                data.append({
                    "username": username,
                    "link": f"https://onlyfans.com/{username}",
                    "contact": "; ".join(contact) if contact else "N/A"
                })
                # 可选：添加短暂延时，模拟人工操作，避免被检测为机器人
                time.sleep(1)

            # 写入CSV
            csv_filename = f"user_data_{page_number}.csv"
            write_to_csv(data, csv_filename, usedata_folder)

            # 处理分页
            try:
                paginators = driver.find_elements(By.CLASS_NAME, "paginator")
                if paginators:
                    next_button = driver.find_element(
                        By.CSS_SELECTOR,
                        'a.fancy-link.fancy-link--kemono.next'  # 保持原有选择器
                    )
                    if "disabled" in next_button.get_attribute("class"):
                        logger.info("“下一页”按钮已禁用，已到达最后一页。")
                        break
                    next_button.click()
                    logger.info("点击“下一页”按钮，等待页面加载。")
                    time.sleep(2)  # 保持原有等待时间
                else:
                    logger.warning("未找到分页元素，已结束。")
                    break
            except NoSuchElementException:
                logger.warning("未找到“下一页”按钮，可能已到达最后一页。")
                break
            except Exception as e:
                logger.error(f"处理分页时发生错误: {e}")
                break

    except Exception as e:
        print(f"主流程中发生错误: {e}")
    finally:
        driver.quit()
        print("浏览器已关闭。")

if __name__ == "__main__":
    main()
